<?php
/**
 * Created by PhpStorm.
 * User: allen
 * Date: 2019/3/8
 * Time: 12:48
 */

namespace app\index\model;

use app\admin\model\Lottery;
use think\Model;

class LotteryLog extends Model
{
    protected $pk='id';

    /**
     * @param int $mc_id
     * @param int $mid
     * @param int $out
     * @return array|\PDOStatement|string|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function is_check($mc_id=0,$mid=0,&$out=''){
        if(!$mc_id || !$mid){
            $out = '缺少参数';
            return false;
        }
        $res = $this->where('mid','eq',$mid)
            ->where('mc_id','eq',$mc_id)
            ->whereBetweenTime('create_time',date('Y-m-d',time()),date('Y-m-d',time()) .' 24:00')
            ->count();
        return $res?true:false;
    }

    /**
     *
     * @param $mid
     * @param $aid
     * @param $mc_id
     * @param $lid
     * @param $num
     * @param string $out
     * @return bool
     */
    public function addLog($mid,$aid,$mc_id,$lid,$num,&$out=''){
        $validate = new \app\index\validate\LotteryLog();
        $data = [
            'mid'=>$mid,
            'aid'=>$aid,
            'mc_id'=>$mc_id,
            'lid'=>$lid,
            'create_time'=>time(),
            'num'=>$num,
        ];
        if(!$validate->check($data)){
            $out = $validate->getError();
            return false;
        }
        $id = $this->insert($data);
        if(!$id){
            return false;
        }
        return $this->getLastInsID();
    }


    public function getMoney($id=0,$mid=0,$title='',$remark='',&$out=null){
        $info = $this->get($id);
        if(!$info){
            $out = '中奖信息不存在';
            return false;
        }
        $member = (new Member())->get($mid);
        if(!$member){
            $out = '中奖人不存在';
            return false;
        }
        if($info['mid']!=$mid){
            $out = '中奖人与领奖人不一致';
            return false;
        }
        if($info['status']==2){
            $out = '您已经领取了';
            return false;
        }
        $_info = (new Lottery())->get($info['lid']);
        if(!$_info){
           $out = '中奖奖品不存在';
           return false;
        }
        if($_info['num']<=0){
            $out = '奖品没有库存';
            return false;
        }
        $free = $_info['money'] * $info['num'];
        if(!$free){
            $out = '中奖金额不正确';
            return false;
        }
        $order_id = build_order_no();
        $out = [
            'mch_billno'   => $order_id,
            'scene_id'     => 'PRODUCT_2',
            'send_name'    => '天空文化传播',
            're_openid'    => $member['open_id'],
            'total_num'    => 1,
            'total_amount' => $free * 100,
            'wishing'      => $title,
            'act_name'     => '天空文化传播',
            'remark'       => $remark,
        ];
        return true;
    }

    /**
     * 更新订单错误
     * @param $id
     * @param int $type
     * @param string $info
     * @return bool
     */
    public function updateStatus($id,$type=1,$info=''){
        $_info = $this->get($id);
        if(!$_info){
           return false;
        }
        $_info->status = $type;
        $_info->update_time = time();
        $_info->info = $info;
        if(!$_info->save()){
            return false;
        }
        return true;
    }

    /**
     * 更新抽奖数据
     * @param int $id
     * @param string $desc
     * @param null $out
     * @return bool
     * @throws \think\exception\PDOException
     */
    public function decMoney($id=0,$desc='',&$out=null){
        $info = $this->get($id);
        if(!$info){
            $out = '中奖信息不存在';
            return false;
        }
        $mch = (new Mch())->get($info['mc_id']);
        $lottery = (new Lottery())->get($info['lid']);
        $free = $lottery['money'] * $info['num'];
        $mch->dong_money = $mch['dong_money'] - $free;
        $lottery->num = $lottery['num'] -1;
        $lottery->get_num = $lottery['get_num'] + 1;
        $info->status = 2;
        $info->update_time = time();
        $info->info = $desc;
        $this->startTrans();
        $result = $info->save();
        $result1 = $lottery->save();
        $result2 = $mch->save();
        if($result && $result1 && $result2){
            $this->commit();
            return true;
        }else{
            $this->rollback();
            $out = '领取失败';
            return false;
        }
    }

    /**
     * 订单抽奖
     * @param int $order_id
     * @param string $desc
     * @param string $sent
     * @param string $hblist
     * @param null $out
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function decMoneyByOrderId($order_id=0,$desc='',$sent='',$hblist='',&$out=null){
        $info = $this->where('order_id','eq',$order_id)->find();
        if(!$info){
            $out = '中奖信息不存在';
            return false;
        }
        $this->startTrans();
        $mch = (new Mch())->get($info['mc_id']);
        $lottery = (new Lottery())->get($info['lid']);
        if($info['status']!=2){
            $free = $lottery['money'] * $info['num'];
            $mch->dong_money = $mch['dong_money'] - $free;
            $lottery->num = $lottery['num'] -1;
            $lottery->get_num = $lottery['get_num'] + 1;
            $info->status = 2;
            $info->update_time = time();
            $info->info = $desc;
            $info->sent_info = $sent;
            $info->hblist = $hblist?json_encode($hblist):'';
            $result = $info->save();
            $result1 = $lottery->save();
            $result2 = $mch->save();
        }else{
            $result = $info->sent_info = $sent;
            $result1 = $result2 = true;
        }
        if($result && $result1 && $result2){
            $this->commit();
            return true;
        }else{
            $this->rollback();
            $out = '领取失败';
            return false;
        }
    }
}